package com.vast.admin.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vast.admin.annotation.UseToken;
import com.vast.admin.entity.Lawyer;
import com.vast.admin.entity.enums.QueryOrderEnum;
import com.vast.admin.service.impl.LawyerService;
import com.vast.common.annotation.Log;
import com.vast.common.core.domain.entity.SysUser;
import com.vast.common.utils.SecurityUtils;
import com.vast.common.constant.MessageCode;
import com.vast.common.core.domain.Result;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 律师 前端控制器
 * </p>
 *
 * @author zhaohao
 * @since 2019-12-09
 */
@RestController
@Api(value = "律师相关接口", tags = { "律师管理模块" })
@RequestMapping("/manager/lawyer")
public class LawyerController {


	@Autowired
	private LawyerService lawyerService;



	@ApiOperation(value = "查询列表信息", notes = "通过条件查询列表")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", name = "size", value = "分页条数", dataType = "int", defaultValue = "10", required = false),
			@ApiImplicitParam(paramType = "query", name = "current", value = "页码,起始值:1", dataType = "int", defaultValue = "1", required = false),
			@ApiImplicitParam(paramType = "query", name = "listMode", value = "是否集合模式:true不进行分页直接返回所有结果集", dataType = "boolean", defaultValue = "false", required = false),
			@ApiImplicitParam(paramType = "query", name = "queryItems", value = "查询条件,json集合", defaultValue = "account,EQ,13419519796;", dataType = "String", required = false),
			@ApiImplicitParam(paramType = "query", name = "orderField", value = "排序字段,多个','分隔", defaultValue = "create_time", dataType = "String",  required = false),
			@ApiImplicitParam(paramType = "query", name = "order", value = "排序类型: ASC/DESC", dataType = "QueryOrderEnum",  required = false)
			})
	@GetMapping("/page")
	public Result<IPage<Lawyer>> page(@ApiParam(hidden = true) Page<Lawyer> page, boolean listMode, String queryItems, String orderField, QueryOrderEnum order) {
		return new Result<IPage<Lawyer>>(MessageCode.SUCCESS, "获取成功", lawyerService.queryList(page, listMode, queryItems, orderField,order));

	}

	@ApiOperation(value = "添加或更新律师", notes = "添加或更新律师")
	@UseToken
	@PostMapping("/addOrUpdate")
	@Log(title = "添加或更新律师")
	public Result<Lawyer> addOrUpdate(@RequestBody @ApiParam(name="SysRole",value="Role信息",required=true) Lawyer Lawyer) {
		if(Lawyer != null) {
			SysUser manager = SecurityUtils.getLoginUser().getUser();
			boolean flag  = lawyerService.addOrUpdate(manager, Lawyer);
			if(flag){
				return new Result<Lawyer>(MessageCode.SUCCESS, "成功", Lawyer);
			}else{
				return new Result<Lawyer>(MessageCode.ERROR, "失败");
			}
		}else {
			return new Result<Lawyer>(MessageCode.PARAM_ERROR, "参数错误");
		}
	}

	@ApiOperation(value = "通过id获取信息", notes = "通过id获取信息")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "header", name = "token", value = "登录获取的token", dataType = "String", required = true)})
	@GetMapping("/info/{id}")
	@UseToken
	@Log(title = "通过id获取信息")
	public Result<Lawyer> info(@PathVariable("id") Long id) {
		if(id != null && id.longValue() > 0){
			SysUser manager = SecurityUtils.getLoginUser().getUser();
			Lawyer bean = lawyerService.info(manager,id);
			if(bean != null){
				return new Result<Lawyer>(MessageCode.SUCCESS, "获取成功", bean);
			}else{
				return new Result<Lawyer>(MessageCode.ERROR, "获取失败");
			}
		}
		return new Result<Lawyer>(MessageCode.PARAM_ERROR, "id不能为空");
	}

	@ApiOperation(value = "删除律师", notes = "删除律师")
	@UseToken
	@Log(title = "删除律师")
	@DeleteMapping("/delete")
	public Result<Boolean> delete(@RequestBody @ApiParam(name="ids",value="数据id数组,以','分隔",required=true) Long[] ids) {
		if(ids != null) {
			SysUser manager = SecurityUtils.getLoginUser().getUser();
			boolean flag  = lawyerService.delete(manager, ids);
			if(flag){
				return new Result<Boolean>(MessageCode.SUCCESS, "删除成功");
			}else{
				return new Result<Boolean>(MessageCode.ERROR, "删除失败");
			}
		}else {
			return new Result<Boolean>(MessageCode.PARAM_ERROR, "参数错误");
		}
	}

}
